package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpItemInfoDao;
import com.cowain.monitor.dao.PmsForecastPartNumDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.datasources.config.TransactionConfig;
import com.cowain.monitor.entity.ErpItemInfoEntity;
import com.cowain.monitor.entity.ErpSofEntity;
import com.cowain.monitor.service.ErpItemInfoService;
import com.cowain.monitor.service.PmsForecastPartNumService;
import com.cowain.monitor.utils.PmsProjectUtil;
import com.cowain.monitor.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 *  
 *  * @author sunfeng
 *  * @projectName pms-monitor
 *  * @title     ErpItemInfoServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    erp同步料号中间库       
 *  * @date   2020/8/19 13:42  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpItemInfoServiceImpl extends ServiceImpl<ErpItemInfoDao, ErpItemInfoEntity> implements ErpItemInfoService {

    @Autowired
    private PmsForecastPartNumDao forecastPartNumDao;

    @Autowired
    private PmsProjectUtil projectUtil;

    @Override
    @DataSource(name = DataSourceNames.THIRD)
    public List<ErpItemInfoEntity> synchroErpItemInfo() {
        return baseMapper.queryErpItemInfoData();
//         itemInfoEntities;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public List<String> findPmsData() {
        //查询pms数据库数据
        return forecastPartNumDao.findItemId();
    }


    /**
     * 向pms库新增数据
     *
     * @param list 料号数据
     * @return 成功与否
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    @Transactional(value = TransactionConfig.DEFAULT_TX, rollbackFor = RuntimeException.class)
    public R insertItemInfo(ArrayList<ErpItemInfoEntity> list) {
        int count = 0;
        if (null != list || list.size() != 0) {
            long startTime = System.currentTimeMillis();
            List<List<ErpItemInfoEntity>> infoEntitys = projectUtil.split(list, 96);
            for (int i = 0; i < infoEntitys.size(); i++) {
                for (ErpItemInfoEntity erpItemInfoEntity : infoEntitys.get(i)) {
                    String srt = "\uDBC0\uDD39";

                    String name = erpItemInfoEntity.getName().replace(srt, "");
                    erpItemInfoEntity.setName(name);
                }
                forecastPartNumDao.insertItemInfos(infoEntitys.get(i));
                count++;
            }
            long endTime = System.currentTimeMillis();

            float seconds = (endTime - startTime) / 1000F;

            System.out.println(Float.toString(seconds) + " seconds.");
            if (count != infoEntitys.size()) {
                throw new RuntimeException("同步料号失败");
            }
        }
        return R.ok();
    }
}
